package com.honey.comment.grammer;

import java.util.List;

import org.java.plugin.Plugin;
import org.java.plugin.PluginLifecycleException;

import com.honey.core.CorePlugin;
import com.honey.core.Extension;
import com.honey.core.calculator.MappingCalculator;
import com.honey.core.dbmapping.structure.Column;
import com.honey.core.dbmapping.structure.ColumnValidator;
import com.honey.core.dbmapping.structure.EnumType;
import com.honey.core.dbmapping.structure.EnumType.EnumValue;
import com.honey.core.processor.ColumnProcessor;
import com.honey.core.utils.EmptyUtility;
import com.honey.core.utils.ObjectFactory;
import com.honey.core.utils.StringUtility;
import com.honey.general.databases.introspected.GeneralColumnValidator;
import com.honey.general.databases.introspected.GeneralEnum;
import com.honey.general.databases.introspected.GeneralProcedureParameter;
import com.honey.general.databases.introspected.GeneralTableColumn;

/**
 * 数据库comment扩展语法 
 * @author Administrator
 *
 */
public class CommentProcessorExtension extends Extension<Plugin> implements ColumnProcessor{
	
	public CommentProcessorExtension(Plugin belongPlugin) {
		super(belongPlugin);
	}
	
	@Override
	public Column processor(Column column) {
		if( column != null ){
			String comment  = column.getComment();
			if(StringUtility.stringHasValue( comment)){
				DatabaseCommentGrammar grammar = ((ColumnCommentProcessorPlugin)this.getBelongPlugin()).getDatabaseCommentGrammar() ;
				if( grammar != null){
					grammar.parser(comment) ;
					if( column instanceof GeneralTableColumn ) setGrammar((GeneralTableColumn)column, grammar);
					else if( column instanceof GeneralProcedureParameter ) setGrammar((GeneralProcedureParameter)column, grammar);
				}
			}
		}
		return column;
	}
	
	/**
	 * 存储过程参数语法设定
	 * @param procedureParameter
	 * @param grammar
	 */
	private void setGrammar(GeneralProcedureParameter procedureParameter,DatabaseCommentGrammar grammar){		
		String tmp = grammar.getComment() ; 
		if( StringUtility.stringHasValue(tmp) ){
			procedureParameter.setComment(tmp);
		}
		tmp =  grammar.getEntityName();
		if( StringUtility.stringHasValue(tmp) ){
			procedureParameter.setJavaProperty(tmp);
		}
		ColumnValidator[] cv =  grammar.getValidator();
		if( ! EmptyUtility.isObjectArrayEmpty(cv) ){
			procedureParameter.setColumnValidator((GeneralColumnValidator[]) cv);
		}
		
		EnumType enumType = grammar.getEnumType();
		if( enumType!= null ){
			List<EnumValue>  list = enumType.getEnums() ;
			if( ! EmptyUtility.isListEmpty(list) ){
				GeneralEnum  generalEnum = new GeneralEnum();
				generalEnum.setEnums(list);
				generalEnum.setComment(procedureParameter.getComment());
				generalEnum.setName(procedureParameter.getName());
				generalEnum.setFullyQualifiedJavaType(procedureParameter.getFullyQualifiedJavaType());
				//char s = generalTableColumn.getName().charAt(0);
				//if( s>=0x30 && s<= 0x39 ){
				//	generalEnum.setJavaProperty("Enum"+generalTableColumn.getName());
				//}else{
					MappingCalculator mappingCalculator = getMappingCalculator();
					generalEnum.setJavaProperty(procedureParameter.getJavaProperty());
				//}
				procedureParameter.setEnumSchema(generalEnum);
			}
		}
	}
	
	/**
	 * 列语法设定
	 * @param generalTableColumn
	 * @param grammar
	 */
	private void setGrammar(GeneralTableColumn generalTableColumn,DatabaseCommentGrammar grammar){		
		String tmp = grammar.getComment() ; 
		if( StringUtility.stringHasValue(tmp) ){
			generalTableColumn.setComment(tmp);
		}
		tmp =  grammar.getEntityName();
		if( StringUtility.stringHasValue(tmp) ){
			generalTableColumn.setJavaProperty(tmp);
		}
		tmp =  grammar.getVirtualDelete();
		if( StringUtility.stringHasValue(tmp) ){
			generalTableColumn.setVirtualDeleteValue(tmp);
		}
		ColumnValidator[] cv =  grammar.getValidator();
		if( ! EmptyUtility.isObjectArrayEmpty(cv) ){
			generalTableColumn.setColumnValidator((GeneralColumnValidator[]) cv);
		}
		
		EnumType enumType = grammar.getEnumType();
		if( enumType!= null ){
			List<EnumValue>  list = enumType.getEnums() ;
			if( ! EmptyUtility.isListEmpty(list) ){
				GeneralEnum  generalEnum = new GeneralEnum();
				generalEnum.setEnums(list);
				generalEnum.setComment(generalTableColumn.getComment());
				generalEnum.setName(generalTableColumn.getName());
				generalEnum.setFullyQualifiedJavaType(generalTableColumn.getFullyQualifiedJavaType());
				//char s = generalTableColumn.getName().charAt(0);
				//if( s>=0x30 && s<= 0x39 ){
				//	generalEnum.setJavaProperty("Enum"+generalTableColumn.getName());
				//}else{
					MappingCalculator mappingCalculator = getMappingCalculator();
					generalEnum.setJavaProperty(generalTableColumn.getJavaProperty());
				//}
				generalTableColumn.setEnumSchema(generalEnum);
			}
		}
	}
	
	private MappingCalculator getMappingCalculator(){
		MappingCalculator answer = null ;
		try {
			answer =((CorePlugin)ObjectFactory.getRequestPlugin(this.getBelongPlugin(), "honey.core.plugin")).getMappingCalculator();
		} catch (PluginLifecycleException e) {
			e.printStackTrace();
		}
		
		return answer;
	}
	
	@Override
	public void clean() {
		
	}
}
